<?php
	# init session
	session_start();

/*  LOAD SYSTEM FUNCTIONS MANUALLY
    ============================================================================================================== */
	require_once( "functions/inc.database.php" );
	require_once( "functions/inc.getData.php" );
	require_once( "functions/inc.pageSetup.php" );
	require_once( "functions/inc.backnavigation.php" );

	if( empty( $_REQUEST["myFlight_login"] ) ) {
		header ("Location: loginForm.php?error=true");
		exit;
	}		
	
	#get user from database
	$sql = "SELECT * FROM userData WHERE username = '".$_REQUEST["myFlight_login"]."'";
	$res = mysql_query($sql);
	if( !$res ) echo mysql_error();
	# check user data
	$usr = mysql_fetch_array( $res );
	
	if( $_REQUEST["myFlight_login"]    == $usr['username'] && 
		$_REQUEST["myFlight_login"]    != ""               && 
		$_REQUEST["myFlight_password"] == $usr['password'] && 
		$_REQUEST["myFlight_password"] != ""               &&
		$usr['hasBeenActivated']       == 1 )
	{
		# Sessionvariablen erstellen und registrieren
		$_SESSION["user_id"]           = $usr['id'];
		$_SESSION["username"]          = $_REQUEST["myFlight_login"];

		# set "last login"
		$sql_ll = "UPDATE `userData` SET `lastLogin` = '" . date( "Y-m-d H:i:s" ) . "' WHERE id = " . $usr['id'];
		$res_ll = mysql_query($sql_ll);
	  
		$userData = getUserData( $usr['id'] );
		$_SESSION["lastLogin"]        = $userData["lastLogin"];
		$_SESSION["gender"]           = $userData["gender"];
		$_SESSION["firstName"]        = $userData["firstName"];
		$_SESSION["lastName"]         = $userData["lastName"];
		$_SESSION["email"]            = $userData["email"];
		$_SESSION["countryCode"]      = $userData["countryCode"];
		$_SESSION["languageCode"]     = $userData["languageCode"];
		
	   	$_SESSION['backNavigation']   = new Navigation();
		
		# redirect to landing-page
		if( empty( $_POST['landing_page'] ) ){
			$landingPage = "index.php?success=true";
		} else {
			$landingPage = ( base64_decode( $_POST['landing_page'] ) );
		}
		header ( "Location: " . $landingPage );
	}
	else {
		echo "ERROR loginCheck#48! Login failed.";
	}
	
?>